home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Presentations / Presentations ’92 / PatchWorks Kit / <PatchWorks++> / ScalperPatch.c < prev    next >
Text File  |  1992-05-15  |  2KB  |  111 lines

  1. /*
  2.     ScalperPatch.c
  3.     
  4.     Performs trap patching using Scalper mechanism.
  5.     
  6.     by Mouse Herrel.
  7.  */
  8.  
  9. #ifndef __GESTALTEQU__
  10. #include <GestaltEqu.h>
  11. #endif
  12. #ifndef __STDIO__
  13. #include <stdio.h>
  14. #endif
  15.  
  16. #ifdef THINK_C
  17. #ifndef __pascal__
  18. #include <Pascal.h>
  19. #endif
  20. #endif
  21.  
  22. #ifdef applec
  23. #ifndef __STRINGS__
  24. #include <Strings.h>
  25. #endif
  26. #endif
  27.  
  28. #include "ScalperPatch.h"
  29. #include "Exceptions.h"
  30. #include "Globals.h"
  31.  
  32. ScalperHook    ScalperPatch::theirHook = 0;
  33.  
  34. ScalperPatch::ScalperPatch()
  35. {
  36.     if (!theirHook)
  37.         InstallScalper();
  38. }
  39.  
  40. ScalperPatch::~ScalperPatch()
  41. {
  42.     // only Scalper patches are safe to replace with the old address.
  43.     if (itsInstalled) {
  44.         Set(itsOld);
  45.         itsInstalled = false;    
  46.     }
  47. }
  48.  
  49. PatchProcPtr ScalperPatch::Get()
  50. {
  51.     return *((*theirHook)(itsTrap));
  52. }
  53.  
  54. void ScalperPatch::Set(PatchProcPtr proc)
  55. {
  56.     *((*theirHook)(itsTrap)) = proc;
  57. }
  58.  
  59. void ScalperPatch::InstallScalper()
  60. {
  61.     ScalperHook scalperHook;
  62.     PatchVectorPtr dcmdHook;
  63.     
  64.     // see if scalper was already found.
  65.     if (theirHook)
  66.         return;
  67.     
  68.     // see if scalper is lurking out there.
  69.     RememberGlobals();
  70.     if (Gestalt(gestaltScalperPatchProc, (long*)&scalperHook) != noErr)
  71.         throw (eNoScalperErr);
  72.     theirHook = scalperHook;
  73.     dcmdHook = (*theirHook) (0);
  74.  
  75.     // write our hook in over the currently installed one.
  76.     // we should work out a way to have concurrent use of Scalper
  77.     // by more than one process. I suppose one way, is to save the
  78.     // old one, and restore it when done, or just allow a way to register
  79.     // several processes by name! Good idea in fact.
  80.     *dcmdHook = (PatchProcPtr)&Unpatch;
  81. }
  82.  
  83. void ScalperPatch::RemoveScalper()
  84. {
  85.     if (!theirHook)
  86.         return;
  87.     
  88.     Unpatch();
  89.     *(*theirHook)(0) = 0;
  90.     theirHook = 0;
  91. }
  92.  
  93. pascal StringPtr ScalperPatch::Unpatch()
  94. {
  95.     StringPtr retVal;
  96.     
  97.     OpenGlobals();
  98.     retVal = "\pAll patches have been removed.";
  99.     try {
  100.         Patch::RemoveAll();
  101.     } catch {
  102.         static char    message[256];
  103.         
  104.         sprintf(message, "An error of type %hd occurred while unpatching.", theException);
  105.         retVal = c2pstr(message);
  106.     }
  107.     
  108.     CloseGlobals();
  109.     return retVal;
  110. }
  111.